package com.relife.sys.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.relife.common.model.AjaxJson;
import com.relife.common.model.Page;
import com.relife.common.utils.StringUtil;
import com.relife.core.easyui.Easyui;
import com.relife.core.easyui.Tree;
import com.relife.core.entity.FunctionEntity;
import com.relife.core.qbc.QbcQuery;
import com.relife.sys.service.FunctionService;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
@RequestMapping("sys/function")
public class FunctionController extends BaseController {
	@Autowired
	private FunctionService funService;

	/**
	 * 页面
	 * 
	 * @author WU
	 * @date 2015-5-20 下午7:54:20
	 * @return
	 */
	@RequestMapping("/functionList")
	public String functionList() {
		return "sys/function/functionList";
	}

	/**
	 * 数据列表
	 * 
	 * @author WU
	 * @date 2015-5-20 下午7:54:08
	 * @param page
	 */
	@RequestMapping("/listData")
	public void listData(String id, Page page, HttpServletResponse res) {
		QbcQuery qbc = new QbcQuery(FunctionEntity.class);
		if (StringUtil.isEmpty(id)) {
			qbc.add(Restrictions.isNull("parent.id"));
		} else {
			qbc.add(Restrictions.eq("parent.id", id));
		}
		funService.getPage(qbc, page);
		Easyui.treegrid(page, res);
	}

	/**
	 * 
	 * @author WU
	 * @date 2016年5月25日 上午10:18:54
	 * @param fun
	 * @param req
	 * @return
	 */
	@RequestMapping("/function")
	public String function(FunctionEntity fun, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(fun.getId())) {
			fun = funService.get(FunctionEntity.class, fun.getId());
		}
		QbcQuery qbc = new QbcQuery(FunctionEntity.class);
		qbc.add(Restrictions.ne("type", 2));
		List<FunctionEntity> list = funService.getList(qbc);
		req.setAttribute("list", list);
		req.setAttribute("fun", fun);
		return "sys/function/function";
	}

	@ResponseBody
	@RequestMapping("/save")
	public AjaxJson save(FunctionEntity fun) {
		AjaxJson j = new AjaxJson();
		try {
			funService.saveFunction(fun);
		} catch (Exception e) {
			e.printStackTrace();
			j.setException(e);
		}
		return j;
	}

	@ResponseBody
	@RequestMapping("/del")
	public AjaxJson del(FunctionEntity fun) {
		AjaxJson j = new AjaxJson();
		try {
			funService.delFunction(fun);
		} catch (Exception e) {
			e.printStackTrace();
			j.setException(e);
		}
		return j;
	}

	@RequestMapping("/tree")
	public void tree(HttpServletResponse res, String id) {
		QbcQuery qbc = new QbcQuery(FunctionEntity.class);
		if (StringUtil.isEmpty(id)) {
			qbc.add(Restrictions.isNull("parent"));
		} else {
			qbc.add(Restrictions.eq("parent.id", id));
		}
		qbc.addOrder(Order.asc("orderBy"));
		List<FunctionEntity> list = funService.getList(qbc);
		Easyui.tree(list, new Tree("id", "name", "children"), res);
	}
}
